package be.redo.model.entities;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * @author Frank
 * 
 */
@Entity
@NamedQueries({
		@NamedQuery(name = "findAantalLines", query = "select count(l) from Line l"),
		@NamedQuery(name = "findAllLines", query = "select l from Line l order by l.departure"),
		@NamedQuery(name = "findLinesByDeparture", query = "select l from Line l where l.departure =  :departure "),
		@NamedQuery(name = "findLinesByDestination", query = "select l from Line l where l.destination =  :destination "),
		@NamedQuery(name = "findLinesByDepartureAndDestination", query = "select l from Line l where l.departure =  :departure and l.destination =  :destination ") })
public class Line implements Serializable {

	/**
	 * Generated serialVersionUID
	 */
	private static final long serialVersionUID = -8955002577580818510L;

	@NotNull
	@ManyToOne
	@JoinColumn(name = "departure")
	private Airport departure;

	@NotNull
	@ManyToOne
	@JoinColumn(name = "destination")
	private Airport destination;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "line_id")
	private Long lineId;

	public Long getLineId() {
		return this.lineId;
	}

	public void setLineId(Long id) {
		this.lineId = id;
	}

	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this,
				ToStringStyle.SHORT_PREFIX_STYLE);
	}

	public Airport getDeparture() {
		return this.departure;
	}

	public void setDeparture(Airport departure) {
		this.departure = departure;
	}

	public Airport getDestination() {
		return this.destination;
	}

	public void setDestination(Airport destination) {
		this.destination = destination;
	}

	/**
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((departure == null) ? 0 : departure.hashCode());
		result = prime * result
				+ ((destination == null) ? 0 : destination.hashCode());
		return result;
	}

	/**
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		Line other = (Line) obj;
		if (departure == null) {
			if (other.departure != null) {
				return false;
			}
		} else if (!departure.equals(other.departure)) {
			return false;
		}
		if (destination == null) {
			if (other.destination != null) {
				return false;
			}
		} else if (!destination.equals(other.destination)) {
			return false;
		}
		return true;
	}

}
